<?php
ob_start();
//------------------------------------------------------------------------------
// Modification       :
//------------------------------------------------------------------------------
// Date(yyyy.mm.dd)   Developer                       Action
//------------------------------------------------------------------------------
// 2007.03.05         Oanhhtk                      Created
//------------------------------------------------------------------------------
// 2007.06.08         Oanhhtk                      Update
//------------------------------------------------------------------------------
// 2007.06.18         Oanhhtk                      Update
//------------------------------------------------------------------------------
// 2007.06.20         Oanhhtk                      Update
//------------------------------------------------------------------------------
// 2008.01.31 		  Oanhhtk 						  Update
//------------------------------------------------------------------------------
// show menu


function getCurentLang(){
	global $LANG;
	if(! IS_ADDMIN_PAGE){
	 	 $lg = $_SESSION["Pkd_LN"];
		 
		switch ($lg){
			/*case "_en" :
				return $LANG["home_page"]["tieng_anh"];
			break;		*/
			case "_en":
				return $LANG["home_page"]["tieng_anh"];
			break;
			/*case "_en":	
				return $LANG["home_page"]["tieng_anh"];	
				break;*/
			default : // EN		
				//return $LANG["home_page"]["tieng_viet"];
				return $LANG["home_page"]["tieng_viet"];
		}
		
	}else {
		$lg=  $_SESSION["Pkd_LN_ADDMIN"];
		
		switch ($lg){
			
			case "_en":
				return "Anh" ;
				break;	
			
			default : // VN			
				//return "Việt";
				return "Việt";
		}
		
	}
}

function createOneMenu($url,$text,$on=false,$space=""){
	if($on) $class = "leftmenu_normal_on";
	else 	$class = "leftmenu_normal";
	?>		 	
	 <TR> 
	  <TD CLASS="<?=$class ?>" onclick="menu_onclick('<?=$url?>')"  onMouseOut="menu_onmouseout(this)" onmouseover="menu_onmouseover(this)" >
		&nbsp;<?=$space?><img src="../images/TriRBlue.gif" width="9" height="9"/>
		<A CLASS="leftmenu" HREF="<?=$url?>" > <?=$text?></A>
	  </TD>
	 </TR>
	<?php
}


function CM_replaceCharJapan($values){
	global $ARRAY_CHAR_JAPAN;
	if(is_array($values)){
		foreach ($values as $key=>$value){
			$values[$key] = CM_replaceCharJapan($value);
		}
		return $values;
	}
	// if not is array
	foreach ($ARRAY_CHAR_JAPAN as $key=>$value){
		//echo $value."-";
		//$values = str_replace($value,"\\".$value."\\",$values);
		$values = str_replace($value,$value."\\",$values);
	}
	return $values;
}
// CM_replaceCharJapan2
function CM_replaceCharJapan2($values){
	global $ARRAY_CHAR_JAPAN;
	if(is_array($values)){
		foreach ($values as $key=>$value){
			$values[$key] = CM_replaceCharJapan2($value);
		}
		return $values;
	}
	// if not is array
	foreach ($ARRAY_CHAR_JAPAN as $key=>$value){
		//echo $value."-";
		//$values = str_replace($value,"\\".$value."\\",$values);
		$values = str_replace($value."\\",$value,$values);
	}
	return $values;
}
// CM_replaceCharJapan4Search
function CM_replaceCharJapan4Search($values){

	global $ARRAY_CHAR_JAPAN;
	if(is_array($values)){
		foreach ($values as $key=>$value){
			$values[$key] = CM_replaceCharJapan4Search($value);
		}
		return $values;
	}
	// if not is array
	foreach ($ARRAY_CHAR_JAPAN as $key=>$value){
		//echo $value."-";
		$values = str_replace($value,$value."\\\\",$values);
	}
	return $values;
}
// CM_replaceCharJapan4Search
function CM_replaceCharJapan2Search($values){

	global $ARRAY_CHAR_JAPAN;
	if(is_array($values)){
		foreach ($values as $key=>$value){
			$values[$key] = CM_replaceCharJapan2Search($value);
		}
		return $values;
	}
	// if not is array
	foreach ($ARRAY_CHAR_JAPAN as $key=>$value){
		//echo $value."-";
		$values = str_replace($value,$value."\\\\\\",$values);
	}
	return $values;
}
//echo CM_replaceCharJapan("分類不能の産業");
// COnvert Uft-8 to shitf_JISS
function CM_getParameterToShift_jis($parameterName){
	global $_GET;
	global $_POST;
	$n			   = $_GET[$parameterName];
	if(empty($n)) $n = $_POST[$parameterName];
	//return trim($n);
	return  iconv("utf-8","shift_jis", $n);
}

function CM_convertArrayToShift_jis($array){

	foreach ($array as $key=>$value){
		$array[$key] = iconv("utf-8","shift_jis",$value);
	}
	return 	$array;
}
//function dinh dang ngay thang
function CM_dateTime($datetime){
	global $datetime;
	$formatdate=substr($datetime,8,2)."/".substr($datetime,5,2)."/".substr($datetime,0,4);
	return $formatdate;
}
function CM_getParameter($parameterName){
	global $_GET;
	global $_POST;
	$n			   = $_GET[$parameterName];
	if(empty($n)) $n = $_POST[$parameterName];
	return trim($n);
}
function CM_redirect($url){
		header("location: $url");
		exit();
}
function CM_gotoPage($url,$t=0){
	$str = ' <script language = "JavaScript" >
			location = "'.$url.'";
		</script>';
	if($t)return $str;
	echo $str;
}

function CM_showError(){
	global $arrayError;

	if (!$arrayError){
		return NULL;
	}
	echo '<div  style="color:#FF0000; padding-top:10px; padding-bottom:10px; "> <center>  ';
	foreach ($arrayError as $error){
		echo " $error ";
	}
	echo '</center> </div>';
	unset($arrayError );
	global $arrayError ;
	$arrayError =  array();
}

function CM_showMessage(){
	global $arrayMessage;

	if (!$arrayMessage){
		return NULL;
	}
	echo '<div  style="background-color:#33CCFF; color:#FFFFFF ">  <center>  ';
	foreach ($arrayMessage as $message){
		echo " $message ";
	}
	echo '</center> </div>';
	unset($arrayMessage );
	global $arrayMessage ;
	$arrayMessage =  array();
}



function CM_countError(){
	global $arrayError;	
	return count($arrayError);
}


//layDuongDanThuMucMe
function CM_getDirParen(){
	$url = $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']) ;
	$str = substr($url,0,strrpos($url,'/'));
	return $str;
}
function  CM_upload($tenFileUpload,$duongDan,$duoiChoPhep='gif,bmp,jpg,GIF,BMP,JPG'){
	global $_FILES;
	global $HTTP_POST_VARS ;
	$duoi3 = substr($_FILES[$tenFileUpload]['name'],strlen($_FILES[$tenFileUpload]['name'])-3,3);
	// kiem tra
	if(! strstr($duoiChoPhep,$duoi3)) return false;
	if( move_uploaded_file($_FILES[$tenFileUpLoad],'abctempl')) return false;
	if(! copy($_FILES[$tenFileUpload]['tmp_name'],$duongDan)) return false;
	return  true;
}
function CM_saveFile($tenFile,$str,$mode='w'){
	$f = fopen($tenFile,$mode);
	if(!$f) return false;
	if(fwrite($f,$str)) {
		fclose($f);
		return true;
	}
	return false;
}
	function CM_form_post_back(){
		return "<form class='form' action='?" . htmlspecialchars($_SERVER['QUERY_STRING']) . "' method='post'>";
	}

function CM_createOption($listValues,$valueSelected=""){
		foreach  ($listValues as $row)
		{
			$id=$row[0];
			if($id==$valueSelected)
			{
				echo"<option value=\"$id\" selected>$row[1]</option>";
			}else{
				echo"<option value=\"$id\">$row[1]</option>";
			}
		} // end foreach  ($arrayData_ttDauTu as $row)
}
	function link_to($linktext, $controller, $action="index", $suffix=NULL, $html_attributes=""){
		$html = "<a href='?controller=$controller&amp;action=$action";

		if ($suffix!=NULL){
			$html .= htmlspecialchars("&$suffix");
		}

		$html .= "'";

		$html .= " $html_attributes ";

		$html .= ">$linktext</a>";

		return $html;
	}

	function link_to_confirm($linktext, $controller, $action="index", $suffix=NULL){
		$html = link_to($linktext, $controller, $action, $suffix, "onclick=\"return confirm('Are you sure?');\"");
		return $html;
	}

	function image_tag($name){
		return "<img src='/images/$name' alt='" . preg_replace("/\\..+/", "", $name) . "' />";
	}

	# Prints the errors in an object as a list
	function error_messages_for($object){
		global $$object;
		$obj = $$object;

		$html = "";

		$html .= "<ul class='errors'>";

		foreach ($obj->_errors as $e){
			$html .= "<li>$e</li>";
		}

		$html .= "</ul>";

		return $html;
	}

	function suppress_field_error(){
		global $_field_errors;

		$_field_errors = FALSE;
	}

	# Return the name of a field as should be inserted into the html
	function _field_name($object, $field){
		if (strpos($object, "[]")===FALSE){
			return $object . "[$field]";
		}else{
			$object = str_replace("[]", "", $object);

			global $$object;

			return $object . "[" . $$object->id . "][$field]";
		}
	}

	# Return the value of a field
	function _field_value($object, $field){
		$object = str_replace("[]", "", $object);

		global $$object;
		$obj = $$object;
		return @$obj->$field;
	}

	function check_box($object, $field, $html_attributes=""){
		$html = "<input type='checkbox' class='check' value='Yes' name='" . _field_name($object, $field) . "'";

		if (_field_value($object, $field)=="Yes"){
			$html .= " checked='checked' ";
		}

		$html .= $html_attributes;

		$html .= " />";

		$html .= error_message_for($object, $field);

		return $html;
	}

	function radio_button($object, $field, $value, $html_attributes=""){
		$html = "<input id='" . strtolower("$object-$field-$value") . "' type='radio' class='radio' value='" . $value . "' name='" . _field_name($object, $field) . "' ";

		if (_field_value($object, $field)==$value){
			$html .= "checked='checked'";
		}

		$html .= $html_attributes;

		$html .= " />";

		$html .= error_message_for($object, $field);

		return $html;
	}

	function text_field($object, $field, $html_attributes=""){
		$html = "<input type='text' value='" . _field_value($object, $field) . "' name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= " />";

		$html .= error_message_for($object, $field);

		return $html;
	}

	function text_area($object, $field, $html_attributes=""){
		$html = "<textarea name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= ">";
		$html .= _field_value($object, $field);
		$html .= "</textarea>";
		$html .= error_message_for($object, $field);
		return $html;
	}
	function hidden_field($object, $field, $html_attributes=""){
		$html = "<input type='hidden' value='" . _field_value($object, $field) . "' name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= " />";

		return $html;
	}
	function password_field($object, $field, $html_attributes=""){
		$html = "<input type='password' name='" . _field_name($object, $field) . "' ";
		$html .= $html_attributes;
		$html .= " />";
		$html .= error_message_for($object, $field);
		return $html;
	}
	
	function select($object, $field, $values){
		$values[""]="";
		$html = "<select name='" . _field_name($object, $field) . "'>";
		foreach ($values as $key=>$value){
			$html .= "<option ";
			# If no keys are supplied - we use the value as the key
			if (is_numeric($key)){
				$key = $value;
			}
			if ($key==_field_value($object, $field)){
				$html .= "selected='selected'";
			}
			$html .= " value='$key'";

			$html .= ">$value</option>";
		}
		$html .= "</select>";
		$html .= error_message_for($object, $field);
		return $html;
	}
function to_money($string)
{
$Negative = 0;

if(preg_match("/^\-/",$string))
{
$Negative = 1;
$string = preg_replace("|\-|","",$string);
}

$string = preg_replace("|\,|","",$string);
$Full = split("[\.]",$string);
$Count = count($Full);
if($Count > 1)
{
$First = $Full[0];
$Second = $Full[1];
$NumCents = strlen($Second);
if($NumCents == 2)
{
}
else if($NumCents < 2)
{
$Second = $Second . "0";
}
else if($NumCents > 2)
{
$Temp = substr($Second,0,3);
$Rounded = round($Temp,-1);
$Second = substr($Rounded,0,2);
}
}
else
{
$First = $Full[0];
$Second = "00";
}
$length = strlen($First);
if( $length <= 3 )
{
//$string = $First . "." . $Second;
$string = $First;
if($Negative == 1)
{
$string = "-" . $string;
}
return $string;
}
else
{
$loop_count = intval( ( $length / 3 ) );
$section_length = -3;
for( $i = 0; $i < $loop_count; $i++ )
{
$sections[$i] = substr( $First, $section_length, 3 );
$section_length = $section_length - 3;
}
$stub = ( $length % 3 );
if( $stub != 0 )
{
$sections[$i] = substr( $First, 0, $stub );
}
$Done = implode( ",", array_reverse( $sections ) );
//$Done = $Done . "." . $Second;
$Done = $Done;
if($Negative == 1)
{
$Done = "-" . $Done;
}

return $Done;
}
}

function getCurrentPageURL($actionLog) {
    //$pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    //$pageURL .= "://";
	$pageURL = '';
	$ipClient = getRealIpAddr();
	$actionTemp = explode('/',$_SERVER["REQUEST_URI"]);
	
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $ipClient.','.$actionLog;
    }
    return $pageURL;
}

function getRealIpAddr()
{
    /*if (!empty($_SERVER['HTTP_CLIENT_IP']))  //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   
    //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;*/
	if ($_SERVER['X_FORWARDED_FOR'] !="")
	{
	$X_FORWARDED_FOR=explode(",", $_SERVER['X_FORWARDED_FOR']);
	$count = count($X_FORWARDED_FOR);
	if($count =0 ) {
	$ip =trim($X_FORWARDED_FOR); // Chỉ có một ip trong chain
	} else {
	$ip=trim($X_FORWARDED_FOR[0]); //Lấy ip đầu tiên trong chain
	}
	} else {
	$ip=$_SERVER['REMOTE_ADDR']; // Người dùng truy cập trực tiếp, không qua proxy
	}
	return $ip; // xuất ip thực của người dùng
}

function getCompanyName(){
	$pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
	 $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
	$arrayUrl = explode("/",$pageURL);
	return $arrayUrl[3];
}
?>
	